package cn.edu.bupt.ch4_1.controller;

import cn.edu.bupt.ch4_1.common.Result;
import cn.edu.bupt.ch4_1.entity.Comment;
import cn.edu.bupt.ch4_1.entity.Product;
import cn.edu.bupt.ch4_1.repo.CommentRepository;
import cn.edu.bupt.ch4_1.repo.ProductRepository;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Tag(name = "商品及评论接口")
@RestController
@RequestMapping("/api/v1/product")
public class ProductController {
    final
    ProductRepository productRepository;
    final
    CommentRepository commentRepository;

    public ProductController(ProductRepository productRepository, CommentRepository commentRepository) {
        this.productRepository = productRepository;
        this.commentRepository = commentRepository;
    }

    @Operation(summary = "返回商品列表", description = "返回值为全部的商品列表")
    @GetMapping(path = "/", produces = "application/json")
    public Result<?> listAll() {
        List<Product> products = productRepository.findAll();
        return Result.ok(products);
    }


    @Operation(summary = "添加新的商品", description = "创建成功，返回状态码201")
    @PostMapping(path = "/", consumes = "application/json", produces = "application/json")
    public Result<?>  add(@RequestBody Product product) {
        product = productRepository.save(product);
        return Result.ok(product);
    }

    @Operation(summary = "根据id删除商品", description = "如果不存在，则返回状态码404，如果存在且删除成功，则返回状态码204")
    @DeleteMapping(path = "/{prodId}")
    public Result<?> delete(@PathVariable Long prodId) {
        Optional<Product> product = productRepository.findById(prodId);
        if (product.isPresent()) {
            productRepository.deleteById(prodId);
            return Result.ok();
        } else {
            return Result.error(204, "该商品不存在");
        }
    }

    @Operation(summary = "获取商品评论列表", description = "返回值为商品的评论列表")
    @GetMapping(path = "/{prodId}/comment/", produces = "application/json")
    public Result<?> commentListAll(@PathVariable Long prodId) {
        Optional<Product> product = productRepository.findById(prodId);
        if (product.isPresent()) {
            return Result.ok(product.get().getComments());
        } else {
            return Result.error(204, "该商品不存在");
        }
    }

    @Operation(summary = "为商品添加评论", description = "创建成功，返回状态码201")
    @PostMapping(path = "/{prodId}/comment/", consumes = "application/json", produces = "application/json")
    public Result<?>  commentAdd(@PathVariable Long prodId, @RequestBody Comment comment) {
        Optional<Product> product = productRepository.findById(prodId);
        if (product.isPresent()) {
            comment.setProduct(product.get());
            comment = commentRepository.save(comment);
            return Result.ok(comment);
        } else {
            return Result.error(204, "该商品不存在");
        }
    }

}
